package com.tequila.core.service.impl;

import com.google.common.collect.Lists;
import com.tequila.core.dao.StoreManagerDao;
import com.tequila.core.service.StoreManagerService;
import com.tequila.domain.dto.StoreManagerDto;
import com.tequila.domain.entity.Manager;
import com.tequila.domain.entity.StoreManager;
import com.tequila.domain.query.StoreManagerQuery;
import fe.core.Assert;
import fe.core.BeanHelper;
import fe.core.Pagination;
import fe.core.UserDetail;
import fe.core.springmvc.UserDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StoreManagerServiceImpl implements StoreManagerService,UserDetailService {
    public static final Logger logger = LoggerFactory.getLogger(StoreManagerServiceImpl.class);

    @Resource
    private StoreManagerDao storeManagerDao;

    @Override
    public Pagination<StoreManagerDto> queryPageListByParam(StoreManagerQuery query) {
        Assert.assertNotNull(query);
        int total = storeManagerDao.queryPageCountByParam(query);

        List<StoreManagerDto> list = Lists.newArrayList();
        if (total > 0) {
            list = storeManagerDao.queryPageListByParam(query);
        }
        return new Pagination<>(query, list, total);
    }

    @Override
    public StoreManagerDto queryById(Long id) {
        Assert.assertNotNull(id,"id不能为空!");
        StoreManager storeManager = storeManagerDao.queryById(id);
        StoreManagerDto storeManagerDto = new StoreManagerDto();
        BeanHelper.copyProperties(storeManagerDto,storeManager);
        return storeManagerDto;
    }

    @Override
    public List<StoreManagerDto> queryByIdList(List<Long> idList) {
        Assert.assertNotNull(idList,"idList不能为空!");
        return storeManagerDao.queryByIdList(idList);
    }

    @Override
    public List<StoreManagerDto> queryListByParam(StoreManagerQuery query) {
        Assert.assertNotNull(query,"query不能为空!");
        return storeManagerDao.queryListByParam(query);
    }


    @Override
    public Long insert(StoreManager entity) {
        Assert.assertNotNull(entity,"entity不能为空!");
        storeManagerDao.insert(entity);
        return entity.getId();
    }

    @Override
    public Long save(StoreManager entity) {
        Assert.assertNotNull(entity,"entity不能为空!");
        storeManagerDao.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public int updateById(StoreManager param) {
        Assert.assertNotNull(param,"param不能为空!");
        Assert.assertNotNull(param.getId(),"id不能为空!");
        return storeManagerDao.updateById(param);
    }

    @Override
    public int updateSelectiveById(StoreManager param) {
        Assert.assertNotNull(param,"param不能为空!");
        Assert.assertNotNull(param.getId(),"id不能为空!");
        return storeManagerDao.updateSelectiveById(param);
    }

    @Override
    public int deleteById(Long id) {
        Assert.assertNotNull(id,"id不能为空!");
        return storeManagerDao.deleteById(id);
    }

    @Override
    public int deleteByIdList(List<Long> idList) {
        Assert.assertNotNull(idList,"idList不能为空!");
        return storeManagerDao.deleteByIdList(idList);
    }


    public StoreManagerDto queryStoreManagerByPhone(String phone) {
        Assert.assertNotNull(phone,"手机号不能为空");
        return storeManagerDao.queryByPhone(phone);
    }

    @Override
    public UserDetail getUserDetail(String account) {
        StoreManagerDto storeManagerDto = storeManagerDao.queryByPhone(account);
        UserDetail userDetail = UserDetail.createUserDetail();
        userDetail.setAccount(storeManagerDto.getPhone());
        userDetail.setId(storeManagerDto.getId());
        return userDetail;
    }
}
